package org.iku.dao;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.iku.entity.Actress;
import org.iku.entity.Category;
import org.iku.entity.IndexHistory;
import org.iku.entity.Item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class ItemIndexDAO {
	private Logger logger = Logger.getLogger(ItemIndexDAO.class);
	
	@Autowired
	private SqlSession sqlSession;
	
	/**
	 * 获取增量索引数据
	 * @param index
	 * @param limit
	 * @param date
	 * @return
	 */
	public List<Item> fetchIncreaseData(int index,int limit,Date date){
		Map<String ,Object> params = new HashMap<String, Object>();
		params.put("index", index);
		params.put("limit", limit);
		params.put("date", date);
		return sqlSession.selectList("ItemIndexMapper.fetchIncreaseData",params);
	}
	
	
	/**
	 * 获取最近一次索引更新记录时间
	 * @param rebuild
	 * @return
	 */
	public IndexHistory getLastDate(boolean rebuild,int type){
		
		if(rebuild){
			Calendar rebuildDate = Calendar.getInstance();
			rebuildDate.set(1990, 1, 1);
			IndexHistory indexHistory = new IndexHistory();
			indexHistory.setDate(rebuildDate.getTime());
			return indexHistory;
		}
		Map<String ,Object> params = new HashMap<String, Object>();
		params.put("type", type);
		
		return sqlSession.selectOne("ItemIndexMapper.getLastDate", params);
	}
	
	/**
	 * 记录索引更新时间
	 * @param date
	 */
	public int markDownLastDate(Date date,int type){
		Map<String ,Object> params = new HashMap<String, Object>();
		params.put("date", date);
		params.put("type", type);
		return sqlSession.insert("ItemIndexMapper.markDownLastDate", params);
	}
	
	/**
	 * 获取地区列表
	 * @return
	 */
	public List<Category> getAllCategories(){
		return sqlSession.selectList("ItemIndexMapper.getAllCategories");
	}
	
	/**
	 * 获取所有女優
	 * @return
	 */
	public List<Actress> getAllActresses(){
		return sqlSession.selectList("ItemIndexMapper.getAllActresses");
	}

}

